<template>
  <div class="permissions">
    <p class="addAuth">
      <el-button type="primary"
                 @click="open({filePath:'Layout'})">添加权限</el-button>
    </p>
    <el-tree :data="Routers"
             node-key="id"
             draggable
             highlight-current
             :expand-on-click-node="false"
             class="tree">
      <span class="custom-tree-node"
            slot-scope="{ node, data }">
        <span>{{ data.name }}</span>
        <span class="append">
          <el-button type="text"
                     size="mini"
                     @click="fileRoute=true,rowID=data.id,open(data)">
            添加
          </el-button>

          <el-button type="text"
                     size="mini"
                     @click="rowID=data.id,open({editName:data.name,action:data.action,DialogState:1})">
            编辑
          </el-button>
          <el-button type="text"
                     size="mini"
                     @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>

    <!-- 添加对话框 -->
    <el-dialog :title="DialogState ? '修改权限' : '添加权限'"
               :visible.sync="addVisible"
               v-el-drag-dialog
               @close='close'
               width="30%"
               center>
      <div>
        <el-form ref="form"
                 :model="form"
                 label-width="80px">
          <el-form-item label="权限名称:"
                        prop="name">
            <el-input v-model="form.name"
                      placeholder="请输入权限名称"></el-input>
          </el-form-item>

          <el-form-item label="权限路径:"
                        prop="action">
            <el-input v-model="form.action"
                      placeholder="请输入权限路径"></el-input>
          </el-form-item>
          <el-form-item label="文件路径:"
                        prop="component"
                        v-if='filePath'>
            <el-input v-model="form.component"
                      placeholder="请输入文件路径"></el-input>
          </el-form-item>
          <el-form-item label="是否显示:"
                        prop="status">
            <el-radio-group v-model="form.status">
              <el-radio label="1">显示</el-radio>
              <el-radio label="2">不显示</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addVisible=false">取 消</el-button>
        <el-button type="primary"
                   @click="DialogState ? editor() : add()">{{DialogState ? '修改' : '添加'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
export default {
  name: 'permissions',
  directives: { elDragDialog },
  data () {
    return {
      filePath: false, //添加第一层,不需要填文件路径,隐藏 默认Layout
      addVisible: false,
      rowID: '', //每一行权限的Id
      DialogState: 0, //弹框显示状态, 0-添加  1-编辑
      form: {
        name: '',
        action: '',
        status: '1',
        component: ''
      }
    }
  },

  computed: {
    ...mapGetters(['Routers']),
  },
  methods: {
    async add () {  //添加权限
      try {
        /**
         *  @param form    name 权限名称  action 权限路径  status 是否在左侧导航栏显示 1显示 2不显示    component 前端文件路径
         *  @param rowID   当前行id 
         */
        const res = await this.api.addPermission(Object.assign({}, { ...this.form, id: this.rowID }))
        if (res.code) {
          this.$message({ message: res.msg, type: 'success' })
          this.addVisible = false
          this.$store.dispatch('ChangeRoles', this.$store.getters.roles)
        } else {
          this.$message.error(res.msg)
        }
      } catch (err) {
        console.log(err)
      }
    },

    remove (node, data) {  //删除权限
      this.$confirm('此操作将永久删除改权限, 是否继续?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(async () => {
        const res = await this.api.delPermission({ id: data.id })
        if (res.code == 1) {
          this.$message({ message: res.msg, type: 'success' })
          this.$store.dispatch('ChangeRoles', this.$store.getters.roles)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { })
    },

    async editor () {  //修改权限
      try {
        const res = await this.api.updatePermission(Object.assign({}, { ...this.form, auth_id: this.rowID }))
        if (res.code == 1) {
          this.$message({ message: res.msg, type: 'success' })
          this.addVisible = false
          this.$store.dispatch('ChangeRoles', this.$store.getters.roles)
        } else {
          this.$message.error(res.msg)
        }
      } catch (err) {
        console.log(err)
      }
    },
    open (data) {    //打开弹框
      this.addVisible = true
      const { action, filePath, editName, DialogState } = data
      this.$nextTick(() => {
        this.form = Object.assign({}, { ...this.form, action: action || '', component: filePath || '', name: editName || '' })
        this.DialogState = DialogState || ''
        this.filePath = filePath ? false : DialogState ? false : true
      })
    },
    close () { //关闭弹框清空form表单
      this.$refs.form.resetFields()
    }
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.permissions {
  .addAuth {
    text-align: right;
    padding: 15px 10px;
    margin: 10px 0;
    background-color: white;
  }
  .tree {
    padding: 10px 0;
  }
  .append {
    position: absolute;
    right: 10px;
  }
}
</style>
